/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Almacen;

import Atxy2k.CustomTextField.RestrictedTextField;
import Controlador.Bean.Almacen.Producto;
import Controlador.Bean.Almacen.TipoProducto;
import gui.General.Main;
import gui.RRHH.NuevoEmpleado;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import lib.Validaciones;


/**
 *
 * @author César
 */
public class NuevoProducto extends javax.swing.JInternalFrame {

    /**
     * Creates new form NuevoProducto
     */
    private ArrayList<TipoProducto> tipos;
    private TableModel tb;
    int clic=0;
    String selectedId;
    
    public NuevoProducto(GestionAlmacenes gestAlmacen) {
        initComponents();
        initComp();
    }
    
    private void initComp(){
         tb = new TableModel();
         tblProductos.setModel(tb);
         tblProductos.setRowSelectionAllowed(true);
         txtIdProducto.setEditable(false);
         tipos = Controlador.Service.Almacen.GestionAlmacenes.obtenerTipo();
         cmbTipo.removeAllItems();
         cmbUnidadMedida.removeAllItems();
         cmbUnidadMedida.addItem("Seleccionar");
         cmbUnidadMedida.addItem("Kg.");
         cmbUnidadMedida.addItem("m.");
         cmbUnidadMedida.addItem("gr.");
         cmbUnidadMedida.addItem("Unidades");
         cmbTipo.addItem("Seleccionar");
         for(int i=0; i<tipos.size(); i++){
             cmbTipo.addItem(tipos.get(i).getNombre());
         }
         tblProductos.setDefaultRenderer(JButton.class, new MiRender());
         tblProductos.setDefaultEditor( JButton.class, new MiEditor());
         btnBuscar.setMnemonic(KeyEvent.VK_F2);
         RestrictedTextField restricted = new RestrictedTextField(txtCantidad);
         restricted.setLimit(5);
         restricted.setOnlyNums(true);
         restricted = new RestrictedTextField(txtIdProducto);
         restricted.setLimit(5);
         restricted = new RestrictedTextField(txtNombre);
         restricted.setLimit(50);
         //restricted.setOnlyText(true);
         restricted = new RestrictedTextField(txtPrecioBase);
         restricted.setLimit(5);
         //restricted.setOnlyNums(true);
         restricted = new RestrictedTextField(txtStockMinimo);
         restricted.setLimit(5);
         restricted.setOnlyNums(true);
         txtCantidad.setText("0");
                 
     }
    
    private static NuevoProducto mynp=null;
    
    public static NuevoProducto getInstance(GestionAlmacenes gestAlmacen) {
         if(mynp == null){
            mynp = new NuevoProducto(gestAlmacen);    
            Main.getDesktop().add(mynp); 
            mynp.setVisible(true);
               mynp.toFront();           
                    }
                    else{
                        if (mynp.isClosed()|| !mynp.isVisible()) {
                                mynp = new NuevoProducto(gestAlmacen); 
                                Main.getDesktop().add(mynp);
                                mynp.toFront();
                                mynp.setVisible(true);
                        }
                        else
                            mynp.toFront();
                    }
        
        
        
                return mynp;
    }
    
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    
    String id ="";
    String nombre = "";
    
    public void filtrar(String id, String nombre){
        this.id = id;
        this.nombre = nombre;
        tb.actualizarTabla();
    }
       
    
     class TableModel extends AbstractTableModel{
        
        //Two arrays used for the table data
        String[] columnNames = {"Código", "Nombre", "Descripción"
                        , "Stock Mínimo", "Cantidad", "Unidad Métrica" ,"Precio Base","Tipo",  "Eliminar"};
               
        public List<Producto> data = Controlador.Service.Almacen.GestionAlmacenes.obtenerProductos();
        
        
        public void actualizarTabla(){
            data = Controlador.Service.Almacen.GestionAlmacenes.filtrarProductos(id, nombre);
            fireTableDataChanged();
            id="";
            nombre="";
        }
        
        @Override
        public int getRowCount()
        {
            return data.size();
        }
        
        
        
        @Override
        public int getColumnCount()            
        {
            return columnNames.length;
        }
        
        @Override
        public Object getValueAt(int row, int column)
        {   
            switch(column){
                case 0: return data.get(row).getIdProducto();
                case 1: return data.get(row).getNombre(); 
                case 2: return data.get(row).getDescripcion();
                case 3: return data.get(row).getStockMinimo();
                case 4: return data.get(row).getCantidad();
                case 5: return data.get(row).getUnidadMetrica();
                case 6: return data.get(row).getPrecioBase();
                case 7: return data.get(row).getIdTipoProducto();
                default: return new JButton("Eliminar");
            }
        }
        
        //Used by the JTable object to set the column names
        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }
        
        //Used by the JTable object to render different
        //functionality based on the data type
        @Override
        public Class getColumnClass(int c) {
            if(c!=8){
            return getValueAt(0, c).getClass();
            }else{
                return JButton.class;
            }
        }
        
        @Override
        public boolean isCellEditable(int row, int column)
        {
	        return false;
        }
        
     }
     
     public class MiRender implements TableCellRenderer {
        // Este metodo indica como debe de pintarse el elemento en la fila row, en la columna column
        // que esta en la tabla table y tiene el color dado por el objeto.
        @Override
        public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) {    
            // como el boton y el calendario son componentes esto se vale.
            return (JComponent)table.getValueAt(row, column);
        }
     }
     
     public class MiEditor extends AbstractCellEditor implements TableCellEditor {
         private Boolean currentValue;
         @Override
         public Object getCellEditorValue()
         {
            return currentValue;
         }
         //El editor usara el propio componente. Para que funcione la celda en el modelo debe ser editable.
         @Override
         public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
         {
           return (JComponent) value;// la tabla solo debe tener componentes graficos
         }
     }
    
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlProducto = new javax.swing.JPanel();
        lblNombre = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        lblDescripcion = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDescripcion = new javax.swing.JTextArea();
        lblCantidad = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();
        lblUm = new javax.swing.JLabel();
        cmbTipo = new javax.swing.JComboBox();
        lblStockMinimo = new javax.swing.JLabel();
        txtStockMinimo = new javax.swing.JTextField();
        btnGuardar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtIdProducto = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cmbUnidadMedida = new javax.swing.JComboBox();
        btnBuscar = new javax.swing.JButton();
        txtPrecioBase = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        pnlProductos = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProductos = new javax.swing.JTable();

        setClosable(true);
        setMaximumSize(new java.awt.Dimension(600, 450));
        setMinimumSize(new java.awt.Dimension(600, 450));
        setName(""); // NOI18N
        setPreferredSize(new java.awt.Dimension(600, 450));

        pnlProducto.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalles del producto"));

        lblNombre.setText("Nombre");

        lblDescripcion.setText("Descripción");

        txtDescripcion.setColumns(10);
        txtDescripcion.setLineWrap(true);
        txtDescripcion.setRows(5);
        jScrollPane2.setViewportView(txtDescripcion);

        lblCantidad.setText("Cantidad");

        txtCantidad.setEnabled(false);

        lblUm.setText("Tipo de Producto");

        cmbTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        lblStockMinimo.setText("Stock Mínimo");

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        jLabel1.setText("IdProducto");

        txtIdProducto.setEditable(false);

        jLabel2.setText("Unidad de Medida");

        cmbUnidadMedida.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btnBuscar.setLabel("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        jLabel3.setText("Precio Base");

        javax.swing.GroupLayout pnlProductoLayout = new javax.swing.GroupLayout(pnlProducto);
        pnlProducto.setLayout(pnlProductoLayout);
        pnlProductoLayout.setHorizontalGroup(
            pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlProductoLayout.createSequentialGroup()
                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlProductoLayout.createSequentialGroup()
                        .addGap(156, 156, 156)
                        .addComponent(btnGuardar)
                        .addGap(18, 18, 18)
                        .addComponent(btnLimpiar)
                        .addGap(18, 18, 18)
                        .addComponent(btnBuscar))
                    .addGroup(pnlProductoLayout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(pnlProductoLayout.createSequentialGroup()
                                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblNombre)
                                    .addComponent(lblDescripcion)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtNombre)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                                    .addComponent(txtIdProducto))
                                .addGap(43, 43, 43)
                                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblCantidad)
                                    .addComponent(lblUm)
                                    .addComponent(lblStockMinimo)
                                    .addComponent(jLabel2)))
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtPrecioBase)
                            .addComponent(txtCantidad)
                            .addComponent(cmbTipo, 0, 127, Short.MAX_VALUE)
                            .addComponent(txtStockMinimo, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                            .addComponent(cmbUnidadMedida, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(72, Short.MAX_VALUE))
        );
        pnlProductoLayout.setVerticalGroup(
            pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlProductoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtIdProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(cmbUnidadMedida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombre)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCantidad)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblDescripcion)
                    .addGroup(pnlProductoLayout.createSequentialGroup()
                        .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblUm))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtStockMinimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblStockMinimo, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPrecioBase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar)
                    .addComponent(btnLimpiar)
                    .addComponent(btnBuscar))
                .addContainerGap())
        );

        pnlProductos.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        pnlProductos.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        tblProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblProductos.setMinimumSize(new java.awt.Dimension(60, 20));
        tblProductos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblProductosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblProductos);

        pnlProductos.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlProducto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlProductos, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlProductos, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(55, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    
    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        //String IdProducto, String IdTipoProducto, String UnidadMetrica, Double Cantidad, Double StockMinimo, String Descripcion, String Estado
        boolean cantidad = Validaciones.validarRealMg(txtCantidad.getText());
        boolean stockMinimo = Validaciones.validarRealMg(txtStockMinimo.getText());
        boolean precioBase = Validaciones.validarRealMg(txtPrecioBase.getText());
        if(cmbTipo.getSelectedIndex() != 0){
            
        
        }
        if(cmbUnidadMedida.getSelectedIndex() == 0){
            JOptionPane.showMessageDialog(this, "Debe seleccionar una unidad de medida", "error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(!cantidad){
            JOptionPane.showMessageDialog(this, "Debe poner una cantidad", "error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(txtDescripcion.getText().isEmpty()){
            JOptionPane.showMessageDialog(this, "Debe poner una descripcion", "error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(txtNombre.getText().isEmpty()){
            JOptionPane.showMessageDialog(this, "Debe poner un nombre", "error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(!stockMinimo){
            JOptionPane.showMessageDialog(this, "Debe poner un stock mínimo", "error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(!precioBase){
            JOptionPane.showMessageDialog(this, "Debe poner un precioBase", "error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(cmbTipo.getSelectedIndex() == 0){
            JOptionPane.showMessageDialog(this, "Debe seleccionar un tipo", "error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        int resultado = JOptionPane.showConfirmDialog(this, "¿Desea guardar los cambios?", "Información", JOptionPane.YES_NO_OPTION);
        if(resultado == JOptionPane.OK_OPTION){
            Producto np = new Producto(txtIdProducto.getText(),tipos.get(cmbTipo.getSelectedIndex()-1).getIdTipoProducto() ,txtNombre.getText(), (String)cmbUnidadMedida.getSelectedItem() , Double.parseDouble(txtCantidad.getText()), Double.parseDouble(txtStockMinimo.getText()) ,  txtDescripcion.getText(), "1", Double.parseDouble(txtPrecioBase.getText()));
            if(txtIdProducto.getText().isEmpty()){
                Controlador.Service.Almacen.GestionAlmacenes.agregarProducto(np);
            }else{
                Controlador.Service.Almacen.GestionAlmacenes.actualizarProduductos(np);
            }
            txtIdProducto.setText("");
            txtNombre.setText("");
            txtPrecioBase.setText("");
            txtCantidad.setText("");
            txtDescripcion.setText("");
            txtStockMinimo.setText("");
            cmbTipo.setSelectedIndex(0);
            cmbUnidadMedida.setSelectedIndex(0);
            cmbTipo.setEnabled(true);
            JOptionPane.showMessageDialog(this, "Se ha guardado correctamente", "Confirmación", JOptionPane.INFORMATION_MESSAGE);
            tb.actualizarTabla();
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        // TODO add your handling code here:zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
        txtIdProducto.setText("");
        txtNombre.setText("");
        txtCantidad.setText("");
        txtDescripcion.setText("");
        txtStockMinimo.setText("");
        txtPrecioBase.setText("");
        cmbTipo.setSelectedIndex(0);
        cmbUnidadMedida.setSelectedIndex(0);
        cmbTipo.setEnabled(true);
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void tblProductosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblProductosMouseClicked
        // TODO add your handling code here:
        
        int selectedRowIndex = tblProductos.getSelectedRow();
        int selectedColumnIndex = tblProductos.getSelectedColumn();
        if(selectedColumnIndex!=8){
            String selectId = (String) tblProductos.getValueAt(selectedRowIndex, 0);
            if(clic == 1 && selectedId.compareTo(selectId)==0){
                txtIdProducto.setText((String)tblProductos.getValueAt(selectedRowIndex, 0));
                txtNombre.setText((String)tblProductos.getValueAt(selectedRowIndex, 1));
                txtDescripcion.setText((String)tblProductos.getValueAt(selectedRowIndex, 2));
                txtStockMinimo.setText(((Double)tblProductos.getValueAt(selectedRowIndex, 3)).toString());
                txtCantidad.setText(((Double)tblProductos.getValueAt(selectedRowIndex, 4)).toString());
                cmbUnidadMedida.setSelectedItem(tblProductos.getValueAt(selectedRowIndex, 5));
                txtPrecioBase.setText(((Double)tblProductos.getValueAt(selectedRowIndex, 6))!=null?((Double)tblProductos.getValueAt(selectedRowIndex, 6)).toString():"0");
                cmbTipo.setSelectedIndex(Integer.parseInt((String)tblProductos.getValueAt(selectedRowIndex, 7)));
                clic = 0;            
                cmbTipo.setEnabled(false);
            }else if(clic == 0){
                selectedId = selectId;
                clic++;
            }else{
                clic = 0;
            }
        }else{
            int confirmado = JOptionPane.showConfirmDialog(this,"¿Desea eliminar el producto?");              
            if (confirmado == JOptionPane.OK_OPTION ){
                if(Controlador.Service.Almacen.GestionAlmacenes.SePuedeEliminarProducto((String)tblProductos.getValueAt(selectedRowIndex, 0))){
                Controlador.Service.Almacen.GestionAlmacenes.eliminarProductos((String)tblProductos.getValueAt(selectedRowIndex, 0));
                tb.actualizarTabla();
                }
                else{
                    JOptionPane.showMessageDialog(this, "El producto esta comprometido con otras transacciones, no puede eliminarlo");
                }
            }
        }
    }//GEN-LAST:event_tblProductosMouseClicked

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
        BuscarProducto bP = new BuscarProducto(this);
        Main.getDesktop().add(bP);
        bP.setVisible(true);
    }//GEN-LAST:event_btnBuscarActionPerformed

    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbTipo;
    private javax.swing.JComboBox cmbUnidadMedida;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblCantidad;
    private javax.swing.JLabel lblDescripcion;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblStockMinimo;
    private javax.swing.JLabel lblUm;
    private javax.swing.JPanel pnlProducto;
    private javax.swing.JPanel pnlProductos;
    private javax.swing.JTable tblProductos;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextArea txtDescripcion;
    private javax.swing.JTextField txtIdProducto;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtPrecioBase;
    private javax.swing.JTextField txtStockMinimo;
    // End of variables declaration//GEN-END:variables
}
